package com.pai4j.common.util;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Locale;

public class EncodeUtil {
    /**
     * default url encoding.
     */
    private static final String DEFAULT_URL_ENCODING = "UTF-8";

    /**
     * unprintable char code.
     */
    private static final int UNPRINTABLE_CHAR_CODE = 16;

    /**
     * ansi char code.
     */
    private static final int ANSI_CHAR_CODE = 256;

    /**
     * hex.
     */
    private static final int HEX = 16;

    /**
     * unicode length.
     */
    private static final int UNICODE_LENGTH = "\\u0000".length();

    /**
     * ansi length.
     */
    private static final int ANSI_LENGTH = "%FF".length();

    /**
     * protected constructor.
     */
    protected EncodeUtil() {
    }

    /**
     * Hex编码.
     *
     * @param input byte[]
     * @return String
     */
    public static String hexEncode(byte[] input) {
        return Hex.encodeHexString(input);
    }

    /**
     * Hex解码.
     *
     * @param input String
     * @return byte[]
     */
    public static byte[] hexDecode(String input) {
        try {
            return Hex.decodeHex(input.toCharArray());
        } catch (DecoderException e) {
            throw new IllegalStateException("Hex Decoder exception", e);
        }
    }

    /**
     * Base64编码.
     *
     * @param input byte[]
     * @return String
     */
    public static String base64Encode(byte[] input)
            throws UnsupportedEncodingException {
        return new String(Base64.encodeBase64(input), "UTF-8");
    }

    /**
     * Base64编码, URL安全(将Base64中的URL非法字符如+,/=转为其他字符, 见RFC3548).
     *
     * @param input byte[]
     * @return String
     */
    public static String base64UrlSafeEncode(byte[] input) {
        return Base64.encodeBase64URLSafeString(input);
    }

    /**
     * Base64解码.
     *
     * @param input String
     * @return byte[]
     */
    public static byte[] base64Decode(String input) {
        return Base64.decodeBase64(input);
    }

    /**
     * URL 编码, Encode默认为UTF-8.
     *
     * @param input String
     * @return String
     */
    public static String urlEncode(String input)
            throws UnsupportedEncodingException {
        return URLEncoder.encode(input, DEFAULT_URL_ENCODING);
    }

    /**
     * URL 解码, Encode默认为UTF-8.
     *
     * @param input String
     * @return String
     */
    public static String urlDecode(String input)
            throws UnsupportedEncodingException {
        return URLDecoder.decode(input, DEFAULT_URL_ENCODING);
    }


    /**
     * 对应js的escape.
     *
     * @param src String
     * @return String
     */
    public static String escapeJS(String src) {
        int i;
        char j;
        StringBuffer tmp = new StringBuffer();
        tmp.ensureCapacity(src.length() * UNICODE_LENGTH);

        for (i = 0; i < src.length(); i++) {
            j = src.charAt(i);

            if (Character.isDigit(j) || Character.isLowerCase(j)
                    || Character.isUpperCase(j)) {
                tmp.append(j);
            } else if (j < ANSI_CHAR_CODE) {
                tmp.append("%");

                if (j < UNPRINTABLE_CHAR_CODE) {
                    tmp.append("0");
                }

                tmp.append(Integer.toString(j, HEX));
            } else {
                tmp.append("%u");
                tmp.append(Integer.toString(j, HEX));
            }
        }

        return tmp.toString();
    }

    /**
     * 对应js的unescape.
     *
     * @param src String
     * @return String
     */
    public static String unescapeJS(String src) {
        StringBuffer tmp = new StringBuffer();
        tmp.ensureCapacity(src.length());

        int lastPos = 0;
        int pos = 0;
        char ch;

        while (lastPos < src.length()) {
            pos = src.indexOf('%', lastPos);

            if (pos == lastPos) {
                if (src.charAt(pos + 1) == 'u') {
                    ch = (char) Integer.parseInt(
                            src.substring(pos + 2, pos + UNICODE_LENGTH), HEX);
                    tmp.append(ch);
                    lastPos = pos + UNICODE_LENGTH;
                } else {
                    ch = (char) Integer.parseInt(
                            src.substring(pos + 1, pos + ANSI_LENGTH), HEX);
                    tmp.append(ch);
                    lastPos = pos + ANSI_LENGTH;
                }
            } else {
                if (pos == -1) {
                    tmp.append(src.substring(lastPos));
                    lastPos = src.length();
                } else {
                    tmp.append(src.substring(lastPos, pos));
                    lastPos = pos;
                }
            }
        }

        return tmp.toString();
    }

    /**
     * 获取SHA256加密后的字符串
     *
     * @param str
     * @return
     */
    public static String getSHA256String(String str) {
        MessageDigest messageDigest;
        String encodeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(str.getBytes(StandardCharsets.UTF_8));
            encodeStr = byte2Hex(messageDigest.digest());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encodeStr;
    }

    private static String byte2Hex(byte[] bytes) {
        StringBuilder stringBuffer = new StringBuilder();
        String temp = null;
        for (int i = 0; i < bytes.length; i++) {
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1) {
                // 1得到一位的进行补0操作
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }

    public static void main(String[] args) throws Exception {
        String accessKey = "YJZNe1dV7QvXd2BKfVhixFXVgi9cWcQf";
        String secretKey = "ubL3vr7cYona72EV6UNeWeJTtsCEgPPo";
        String nonce = "5477";
        String maskId = "JHrekNQOHLVhfUmx";
        long timestamp = System.currentTimeMillis();
        String encryptStr = "accessKey="+accessKey+"&nonce="+nonce+"&secretKey="+secretKey+"&timestamp="+timestamp;
        // 加密
        String sign = getSHA256String(encryptStr).toUpperCase(Locale.ROOT);
        System.out.println("时间戳："+timestamp+" 加密后的字串是：" + sign);

        System.out.println("url: https://v6-open.51.la/open/online/data?maskId="+maskId+"&type=ACTIVE_USER&minute=15&accessKey="+accessKey+"&nonce=5477&timestamp="+timestamp+"&sign="+sign);
        System.out.println("url: https://v6-open.51.la/open/overview/get?maskId="+maskId+"&type=ACTIVE_USER&minute=15&accessKey="+accessKey+"&nonce=5477&timestamp="+timestamp+"&sign="+sign);

    }
}
